
/*CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
 C
 C   handler_library.c
 C   Application name:
 C   Programmer name: Deepa Sahni
 C   Date completed: 05/15/11
 C   Brief Description: Operations in this library can be accessed only in Kernel mode.
 C
 CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC*/
#include "io_device.c"

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 +	Handler Library
 +
 +
 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#ifndef _HANDLER_LIB
#define _HANDLER_LIB
// Operations

//The handler called by the CPU when the timer generates an interrupt request.
void timer_int_handler()
{
puts("Timer Interrupt occured. \n");
//moves process from front of queue to back of queue
PCBStr *process_pcb = ready_queue_process_remove();
if(process_pcb != NULL)
{
	if((process_pcb->next_step - QUANTUM)> 0)
	{
		printf("Process %s has %d steps remaning.\n", process_pcb->pid->name, (process_pcb->next_step - QUANTUM));
		process_pcb->next_step = process_pcb->next_step - QUANTUM;
		process_pcb->state = 2;//process changed to interrupted
		printf("Process %s status changed to interrupted.\n", process_pcb->pid->name);
		add_pcb_ready_queue(process_pcb);//add pcb to back of the queue
		printf("Process %s added to the back of the queue.\n",  process_pcb->pid->name);
		ready_queue->tail->prev->prev->state = 1;//process changed to ready after being added to the queue
		printf("Process %s status changed to ready.\n", ready_queue->tail->prev->prev->pid->name);

	}
	//timer_tick();
	get_next_process_running(); //gets the process in front of the queue running
}
else
{
	puts("No more processes in queue. Only idle process running.\n");
}
}

//The handler called by the CPU when an IO device completes an action and is requesting service.
void io_int_handler()
{

}

//The handler called by the CPU when the currently running process requests and IO operation.
//Called by the io_system_call()device_type tell you which device the io request is being made.
//For eg 'v' is for video_io and 'd' is for disk io.
void io_req_trap_handler(char device_type)
{
	if(device_type == 'v')
	{
		add_pcb_io_blocked_queue(ready_queue_process_remove(), video_io);
		get_next_process_running();
	}
	else if(device_type == 'd')
	{
		add_pcb_io_blocked_queue(ready_queue_process_remove(), disk_io);
		get_next_process_running();
	}

}

//The handler called by the CPU when sem_down_system_call() is called by the user.
void semaphor_down_trap_handler()
{

}

//The handler called by the CPU when sem_down_system_call() is called by the user.
void semaphor_up_trap_handler()
{

}

//The handler called by the CPU when mem_write_system_call() is called by the user.
void memory_write_trap_handler()
{

}

//The handler called by the CPU when mem_read_system_call() is called by the user.
void memory_read_trap_handler()
{

}
#endif
